package arrAndstr;

/*
    98. 验证二叉搜索树
 */
public class T98 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    private static boolean isBST = true;
    private static long beforeValue = Long.MIN_VALUE;
    public static boolean isValidBST(TreeNode root) {
        // BST的LDR是递增的 --> 用一个数来记录上一个遍历过的数
        validBST(root);

        return isBST;
    }

    private static void validBST(TreeNode root) {
        if (root == null)   return;

        validBST(root.left);

        if (root.val <= beforeValue) {
            isBST = false;
            return;
        }
        beforeValue = root.val;

        validBST(root.right);
    }

    public static void main(String[] args) {
        TreeNode node = new TreeNode(2);
        node.left = new TreeNode(1);
        node.right = new TreeNode(3);

        System.out.println(isValidBST(node));
    }
}
